programming4us
           
 
 
SQL Server

SQL Server 2008: Administering Database Objects - Working with Tables (part 2) - Primary Key Constraints & Unique Constraints

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
2/8/2011 11:32:24 AM

2. Primary Key Constraints

A primary key is a column or set of columns that can be used to uniquely identify a row in a table. If you use multiple columns to create a primary key, it is referred to as a composite key. Data can be repeated in each individual column in the composite key as long as all of the data elements that make up the composite key are unique for each row in the table.

You can create the primary key when you create the table, or create it later using the ALTER TABLE statement. When you create a primary key, SQL Server automatically creates a unique index to enforce that uniqueness. The unique index will be clustered unless you specify nonclustered, or if there is already a clustered index on the table.

To create a primary key on a column when creating a table, you need to specify the PRIMARY KEY keyword following the column definition. Optionally, you can specify CLUSTERED or NONCLUSTERED to determine the type of index that will be generated. You also have the option to specify the name of the constraint that will be created by using the CONSTRAINT keyword followed by the name of the constraint. If you do not name the constraint, you will be at the mercy of SQL Server and will end up with an auto-generated name like PK__Orders__C3905BAF52793849. Listing 5 shows the syntax for creating a primary key when creating a table.

Example 5. Syntax for Creating a Primary Key When Creating a Table
USE AdventureWorks2008
GO

--Drop the table if it currently exists
IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL
DROP TABLE dbo.Orders;

--Create the Orders table
CREATE TABLE Orders
(OrderID int NOT NULL CONSTRAINT PK_ORDERS PRIMARY KEY CLUSTERED)

To create a primary key after a table has been created, you can use the ALTER TABLE statement. Listing 6 shows how to create a composite key on a table after the table has already been created. Since the OrderID will be repeated for each ProductID contained in the Order- Details table for a single order, you can uniquely identify the order using the OrderID and the ProductID combined.

Example 6. Syntax to Create a Composite Key Using the ALTER TABLE Statement
USE AdventureWorks2008
GO

--Drop the table if it currently exists
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL
DROP TABLE dbo.OrderDetails;

--Create the OrderDetails table
CREATE TABLE OrderDetails
(OrderID int NOT NULL,
ProductID int NOT NULL)

--Alter the OrderDetails table to add a composite key
ALTER TABLE OrderDetails
ADD CONSTRAINT PK_Order_Details PRIMARY KEY CLUSTERED
(OrderID,
ProductID)

To remove a primary key from a table, you need to issue the ALTER TABLE statement with the DROP CONSTRAINT keywords followed by the name of the primary key, as shown in Listing 7.

Example 7. Syntax to Remove a Primary Key Constraint
ALTER TABLE OrderDetails DROP CONSTRAINT PK_Order_Details

3. Unique Constraints

You can use a unique constraint to maintain distinct values in a column or set of columns that do not participate in the primary key. As with a primary key, when you create a unique constraint, SQL Server automatically creates a unique index to ensure the column values are distinct. Unlike with a primary key, you can define multiple unique constraints per table. You can also define a unique constraint on one or more columns that accept NULL values; however, if you define a unique constraint on single column, that column can accept only one NULL value.

To create a unique constraint on a column when creating a table, you need to specify the UNIQUE keyword following the column definition. Optionally, you can specify CLUSTERED or NONCLUSTERED to determine the type of index that will be generated; NONCLUSTERED is the default. Remember, you can have only one clustered index per table, so if you are using a clustered index for your primary key, the index on the unique constraint must be nonclustered. You also have the option to specify the name of the constraint by using the CONSTRAINT keyword followed by the name of the constraint. Listing 8 shows the syntax for creating a unique constraint when creating a table.

Example 8. Syntax for Creating a Unique Constraint When Creating a Table
USE AdventureWorks2008
GO

--Drop the table if it currently exists
IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL
DROP TABLE dbo.Orders;

--Create the Orders table
CREATE TABLE Orders
(OrderID int NOT NULL CONSTRAINT PK_ORDERS PRIMARY KEY CLUSTERED,
OrderNumber int NULL CONSTRAINT UQ_ORDER_NUMBER UNIQUE NONCLUSTERED)

To create a unique constraint after you have already created a table, you can use the ALTER TABLE statement. Listing 9 shows how to create a unique constraint using multiple columns on an existing table.

Example 9. Syntax to Create a Unique Constraint Using the ALTER TABLE Statement
USE AdventureWorks2008
GO

--Drop the table if it currently exists
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL
DROP TABLE dbo.OrderDetails;

--Create the OrderDetails table
CREATE TABLE OrderDetails
(OrderID int NOT NULL CONSTRAINT PK_ORDERDETAILS PRIMARY KEY CLUSTERED,
OrderNumber int NULL,
CustomerNumber int NOT NULL)

--Alter the OrderDetails table to add the unique constraint
ALTER TABLE OrderDetails
ADD CONSTRAINT UQ_ORDER_CUSTOMER_NBR UNIQUE
(OrderNumber,
CustomerNumber)

To remove a unique constraint from a table, you need to issue the ALTER TABLE statement with the DROP CONSTRAINT keywords followed by the name of the unique constraint, as shown in Listing 10.

Example 10. Syntax to Remove a Unique Constraint
ALTER TABLE OrderDetails DROP CONSTRAINT UQ_ORDER_CUSTOMER_NBR
Other -----------------
- SQL Server 2008: Administering Database Objects - Working with Database Snapshots
- Programming with SQL Azure : WCF Data Services (part 3)
- Programming with SQL Azure : WCF Data Services (part 2) - Creating the Client Application
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 4) - EXPLICIT Mode
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 3) - AUTO Mode
- Programming with SQL Azure : WCF Data Services (part 1)
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 2) - Working with Binary Columns
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 1) - RAW Mode
- Programming with SQL Azure : Connecting to SQL Azure (part 4) - Sqlcmd
- Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us